
# $Id: Enclosure.pm 149 2013-04-13 05:42:04Z chengxin.cai $

use strict;

package SQConfig::Enclosure;

use Switch;
use Carp;

use PerlUtil qw( gen_getter );

sub import {
    my $package = shift;
    $package =~ s/.*:://;

    ##export the constructor
    no strict 'refs';
    my ($caller) = caller;
    *{ $caller . '::' . $package} = \&{$package};

    gen_getter( __PACKAGE__, 'rack' );
    gen_getter( __PACKAGE__, 'nodelist' );
    gen_getter( __PACKAGE__, 'as_string' );
}

sub Enclosure {
    my ($line) = @_;

    my ( $rack, $nodelist ) = split /=/, $line;
    $rack =~ s/enc-//;
    bless sub {
        my $op = shift;

        switch ($op) {
            case 'rack'      { return $rack; }
            case 'nodelist'  { return $nodelist; }
            case 'as_string' { return sprintf "enc-%s=%s", $rack, $nodelist; }
            else             { croak "Invalid operation $op"; }
        }
    }, __PACKAGE__;
}

use overload q{""} => \&as_string;

1;

